package com.le.medium.class7;

import org.junit.Test;

/**
 * 数字中文表达
 * 从小范围依次扩大
 */
public class Problem01_ChineseExpressionTest {

    public static String num1To9(int num) {
        if (num < 1 || num > 9) {
            return "";
        }
        String[] names = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
        return names[num - 1];
    }

    public static String num1To99(int num, boolean hasBai) {
        if (num < 1 || num > 99) {
            return "";
        }
        if (num < 10) {
            return num1To9(num);
        }
        int shi = num / 10;
        if (shi == 1 && (!hasBai)) {
            return "十" + num1To9(num % 10);
        } else {
            return num1To9(shi) + "十" + num1To9(num % 10); // 11 --> 一十一
        }
    }

    public static String num1To999(int num) {
        if (num < 1 || num > 999) {
            return "";
        }
        if (num < 100) {
            return num1To99(num, false);
        }
        int bai = num / 100;
        String res = num1To9(bai) + "百";
        // 特殊点 111 --》 一百一十一
        int rest = num % 100;
        if (rest == 0) { // 100 200, 300
            return res;
        } else if (rest >= 10) {
            // 111 121
            res += num1To99(rest, true);
        } else {
            res += "零" + num1To9(rest); // 103
        }
        return res;
    }

    public static String num1To9999(int num) {
        if (num < 1 || num > 9999) {
            return "";
        }
        if (num < 1000) {
            return num1To999(num);
        }
        int q = num / 1000;
        String res = num1To999(q) + "千"; // 多少个千
        int rest = num % 1000;
        if (rest == 0) {
            return res; // 1000 2000...
        } else if (rest >= 100) {
            res += num1To999(rest);
        } else {
            res += "零" + num1To99(rest, false); // 1080
        }
        return res;
    }


    public static String num1To99999999(int num){
        if (num < 1 || num > 99999999) {
            return "";
        }
        int wan = num / 10000;
        int rest = num % 10000;
        if (wan == 0) {
            return num1To9999(rest); // 100000,200000,30000...
        }
        String res = num1To9999(wan) + "万";
        if (rest == 0){
            return res;
        }else if (rest >= 1000){ // 12344...
            res += num1To9999(rest);
        }else {
            res += "零" + num1To999(rest); // 10001 一万零一
        }
        return res;
    }

    public static String getNumChiExp(int num){
        if (num == 0){
            return "零";
        }
        String res = num < 0 ? "负" : "";
        int yi = Math.abs(num / 100000000);
        int rest = Math.abs((num % 100000000));
        if (yi == 0){
            return num1To99999999(num);
        }
        res += num1To9999(yi) + "亿";
        if (rest == 0){
            return res;
        }else {
            if (rest < 10000000) {
                return res + "零" + num1To99999999(rest); // 一亿零。。。。
            } else {
                return res + num1To99999999(rest); // 一亿三千。。。。
            }
        }
    }

    @Test
    public void test() {
//        System.out.println(num1To9(1));
//        System.out.println(num1To999(108));
        System.out.println(getNumChiExp(10));
        System.out.println(getNumChiExp(110));
        System.out.println(getNumChiExp(1010));
        System.out.println(getNumChiExp(10010));
        System.out.println(getNumChiExp(1900000000));
        System.out.println(getNumChiExp(1000000010));
        System.out.println(getNumChiExp(1010100010));
    }
}
